var labelMainId;
var index=1;
var typeArray;           //绑定动态商品类别
var path = "./";
function onchangLabelMain(value) {
    $("#form_labelMainId").val(value);
    loadLabelTypeByBigType();
}

function loadLabelMain()
{
    $.ajax({
        type : "POST",
        url : path + '/flowerSetting/loadLabelMain.action',
        async : false,
        success : function(data) {
            if (eval(data).length > 0) {
                var html = "";
                $.each(eval(data), function(i)
                {
                    if(labelMainId != undefined && labelMainId != "" && labelMainId != "null")
                    {
                        if(this.id == labelMainId)
                        {
                            html+="<option selected=\"selected\" value=" + this.id + ">" +this.name + "</option>";
                        }else{
                            html+="<option  value=" + this.id + ">" +this.name + "</option>";
                        }
                    }else{
                        if(i == 0)
                        {
                            $("#form_labelMainId").val(this.id);
                        }
                        html+="<option  value=" + this.id + ">" +this.name + "</option>";
                    }
                });

                $("#form_select").html(html);
                //根据大类加载属性
                loadLabelTypeByBigType();
            }
        }
    });
}

//根据mainId加载商品属性
function loadLabelTypeByBigType()
{
    var typeMain = $("#form_labelMainId").val();
    $.ajax({
        url: path + '/goods/loadLabeType.action?id='+typeMain,
        type: 'post',
        async : false, //同步
        success:function(data){
            var html = '';
            datalength=eval(data).length;
            if (eval(data).length > 0) {
                var isok=false;
                $.each(eval(data), function(i) {
                    var option;
                    var selectVal;
                    $.ajax({ type: "post",
                        url: path + "/flowerType1/showFlowerType1.action?mapId="+this.id,
                        data: {},
                        async: false,
                        success: function (data) {
                            var data=jQuery.parseJSON(data);
                            if(data.length>0)
                            {
                                option+='<option value="0" id="zzzzz">请选择</option>';
                                for (var i = 0; i < data.length; i++) {
                                    if(typeArray != undefined && typeArray.length > 0)
                                    {
                                        for(var j=0;j<typeArray.length;j++)
                                        {
                                            if(typeArray[j]==data[i].id)
                                            {
                                                option+="<option selected=\"selected\" value=" + data[i].id + " id="+data[i].abbr+">" + data[i].name + "</option>";
                                                selectVal=data[i].abbr;
                                                isok=true;
                                            }
                                        }
                                        if(isok==false)
                                        {
                                            option+="<option value=" + data[i].id + " id="+data[i].abbr+">" + data[i].name + "</option>";
                                        }
                                        isok=false;
                                    }else{
                                        option+="<option value=" + data[i].id + " id="+data[i].abbr+">" + data[i].name + "</option>";
                                    }
                                }
                            }else
                            {
                                option="";
                            }
                        }
                    });
                    if(option!="")
                    {
                        html+='<div class="am-u-sm-1 am-text-right  am-pad-none" style="margin-bottom:10px;"><span style="font-size : 12px" >'+this.name+':</span></div>'+
                            '<div class="am-u-sm-3 " style="margin-bottom:10px;">'+
                            '<input type="hidden" id="num_'+index+'" value="'+selectVal+'">'+
                            "<select id='"+this.id+"' name='pwxgoods.labelDetailId' class='selectstyle'>'"+option+"'</select>"+
                            '</div>';
                        index=index+1;
                    }
                });
            }
            $("#goodsType").html(html);
            $("#goodsType select").last().parent().addClass("am-u-end");
        }
    });
}

function loadInitGoodsProperty(types, label, url){
    //types 动态商品类别ID集合
    labelMainId = label;
    path = url;
    if(types!="" && types != null)typeArray=types.split(",");
    loadLabelMain();
}